<?php
require (Yii::app ()->basePath . '/lib/rolling-curl/RollingCurl.php');
/**
 *
 * @author Administrator
 * @see 首页控制器
 *
 */
class SiteController extends CController {
	/**
	 *
	 * @see 首页
	 */
	public function actionIndex() {
		if (Yii::app ()->user->isGuest) {
			$this->redirect ( Yii::app ()->user->loginUrl );
		}
		$this->render ( 'index' );
	}
	
	/**
	 *
	 * @see 验证登录
	 */
	public function actionLogin() {
		$this->render ( 'login' );
	}
	public function actionLogout() {
		Yii::app ()->user->logout ();
		$this->redirect ( Yii::app ()->user->loginUrl );
	}
	/**
	 *
	 * @see ajax 验证登录
	 */
	public function actionAjax_login() {
		$ret = new stdClass ();
		$account = $_POST ['act'];
		$pwd = $_POST ['pwd'];
		if (isset ( $account ) === false) {
			$ret->status = 500;
			$ret->data = "用户名不能为空";
			echo json_encode ( $ret );
			return;
		}
		if (isset ( $pwd ) === false) {
			$ret->status = 500;
			$ret->data = "密码不能为空";
			echo json_encode ( $ret );
			return;
		} else {
			// md5加密方式
			sys_debug_log ( $account );
			$lobj_user_identity = new UserIdentity ( $account, $pwd );
			if ($lobj_user_identity->authenticate () === true) {
				sys_debug_log ( $lobj_user_identity->getId () );
				$duration = 3600 * 24 * 7; // cookie 时间
				Yii::app ()->user->login ( $lobj_user_identity, $duration ); // 创建Session,cookie
				$service = new SysAdminEntity ();
				$model = $service->get ( $lobj_user_identity->getId () );
				if (false === $model) {
					// 删除无效的session数据
					Yii::app ()->user->logout ();
					Yii::app ()->session->clear ();
					Yii::app ()->session->destroy ();
					$ret->status = 500;
					$ret->data = "用户与密码不匹配";
					echo json_encode ( $ret );
					return;
				}
				Yii::app ()->session ['user'] = $model; // 保存登录用户信息;
				if (strlen ( $model ['sys_role'] ) > 0)
					Yii::app ()->session ['power'] = explode ( ',', $model ['sys_role'] );
				else
					Yii::app ()->session ['power'] = array ();
				$ret->status = 200;
				$ret->data = "登录成功";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 500;
				$ret->data = "用户与密码不匹配";
				echo json_encode ( $ret );
				return;
			}
		}
	}
}